﻿' NOTE: You can use the "Rename" command on the context menu to change the class name "Service1" in code, svc and config file together.
Public Class RestServiceImpl
    Implements IRestServiceImpl
    Dim r As Random = New Random(10000)
    Public Sub New()
    End Sub

    Public Function ValidarTarjetaCredito(ByVal NumeroTarjeta As [String]) As String Implements IRestServiceImpl.ValidarTarjetaCredito
        Dim arrDigitos As Integer() = New Integer(15) {}
        Dim intDigito As Integer = 0
        Dim blnValida As Boolean = False

        If NumeroTarjeta.Length = 16 Then
            For x As Integer = 0 To arrDigitos.Length - 1
                If Integer.TryParse(NumeroTarjeta.Substring(x, 1), intDigito) Then
                    arrDigitos(x) = intDigito
                    'Si es posición Impar lo multiplicamos por 2
                    If Not ((x + 1) Mod 2 = 0) Then
                        If intDigito * 2 > 9 Then
                            arrDigitos(x) = intDigito * 2 - 9
                        Else
                            arrDigitos(x) = intDigito * 2
                        End If
                    End If
                End If
            Next
            'Sumamos la suma de cifras obtenidas
            Dim sumTotal As Integer = 0
            For x As Integer = 0 To arrDigitos.Length - 1
                sumTotal += arrDigitos(x)
            Next

            If sumTotal Mod 10 = 0 And sumTotal <= 150 Then
                blnValida = True
            Else
                blnValida = False
            End If
        Else
            blnValida = False
        End If

        If blnValida Then
            'Devolvemos el Nùmero de Transacciòn
            Dim aleat1 As Integer = r.Next()
            Return aleat1.ToString()
        End If
        Return String.Empty

    End Function
End Class
